function [euc_dist corr_dist] = fsb_diag_euclid(idat,showfigure)

% FSB : Display euclidian distances
%
% EXAMPLE:
% fsb_diag_euclid(idat,1)
%
% INPUT:
% idat: 4 D image data
% showfigure: set this to one to get output
%
% OUTPUT:
% Diagnostic maps
%
% CALLED BY:
% fsb_diag.m
% fsb_realign.m
%
% NOTES:
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% inspired by David Omer, MPI for Biological Cybernetics
% License:GNU GPL, no express or implied warranties
%
%$ Revision 1.0
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if nargin<2;
    showfigure =1;
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Get image dimensions
%~~~~~~~~~~~~~~~~~~~~~~~~~~

dim = size(idat);

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Transform first volume to vector
%~~~~~~~~~~~~~~~~~~~~~~~~~~
sidat = idat(:,:,:,1);
sidat = double(sidat(:));

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Go over volumes and calculate euclidian distances
%~~~~~~~~~~~~~~~~~~~~~~~~~~
euc_dist(1) = NaN;
corr_dist(1) = NaN;

for x = 2:dim(4);

    nidat = idat(:,:,:,x);
    nidat = double(nidat(:));
    euc_dist(x) = sum(sqrt((sidat-nidat).^2));
    corr_dist(x) = corr(nidat,sidat);

end

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Show output if desired
%~~~~~~~~~~~~~~~~~~~~~~~~~~
if showfigure ==1;

    figure;
    subplot(2,1,1);
    plot(euc_dist,'r');
    ylabel('Euclidian distance');
    xlabel('Volume');
    title('Euclidian distance from first volume');
    subplot(2,1,2);
    plot(corr_dist,'g');
    ylabel('Correlation');
    xlabel('Volume');
    title('Correlation with first volume');

end


disp('Euclidian distances of volumes over time')
end